package validate;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import constant.Msg;
import util.AppUtil;
import model.Recipe;

public class RecipeValidate {
	public static String get(String type, String keyword, String offset, String length) {
		if(type==null && keyword==null){
			return Msg.ERROR_PARA_NUM;
		}
		if(offset!=null && AppUtil.isInteger(offset) && Integer.parseInt(offset)<0)
			return Msg.ERROR_PATTERN;
		if(length!=null && AppUtil.isInteger(length) && Integer.parseInt(length)<=0)
			return Msg.ERROR_PATTERN;
		if(type!=null){
			for (Record record : Db.find("select distinct type from recipes where 1=1;")) {
				if(type.equals(record.get("type"))){
					return null;
				}
			}
			return Msg.ERROR_PATTERN;
		}
		return null;
	}

	public static String delete(String id) {
		if (id == null)
			return Msg.ERROR_PARA_NUM;
		if (!(AppUtil.isInteger(id)))
			return Msg.ERROR_PARA_TYPE;
		if (!(Recipe.dao.selectById(Integer.parseInt(id)) != null))
			return Msg.ERROR_ID_UNEXISTED;
		return null;
	}

	public static String random(String length) {
		if (length != null && !(AppUtil.isInteger(length)))
			return Msg.ERROR_PARA_TYPE;
		return null;
	}

	public static String search(String keyword) {
		if (keyword == null)
			return Msg.ERROR_PARA_NUM;
		return null;
	}
	
	public static String getFilters(String filter) {
		if(filter==null)
			return Msg.ERROR_PARA_NUM;
		if(!"type".equals(filter) && !"region_taste".equals(filter) && !"technology".equals(filter))
			return Msg.ERROR_PATTERN;
		return null;
	}
}
